package com.zelma.house.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.xxl.job.core.handler.annotation.XxlJob;
import com.zelma.house.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zelma.house.entity.ResourceEntity;
import com.zelma.house.service.ResourceService;
import com.zelma.common.utils.PageUtils;
import com.zelma.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author zelma
 * @email 1239115389@qq.com
 * @date 2022-01-07 14:58:16
 */
@RestController
@RequestMapping("house/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @Autowired
    JwtUtil jwtUtil;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws IOException {
        PageUtils page = resourceService.queryPage(params);

        return R.ok().put("page", page);
    }


    @GetMapping("/homeFunEslist")

    public R homeFunEslist(@RequestParam Map<String, Object> params, HttpServletRequest request) throws IOException {
        String token = request.getHeader("token");
        if (token!=null ){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null &&claims.get("roles").equals("agent")){
                PageUtils page = resourceService.homeFunEslist(params);
                return R.ok().put("page", page);
            }
        }
        return R.error("无权限访问！！");
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{resourceId}")
    public R info(@PathVariable("resourceId") Integer resourceId){
		ResourceEntity resource = resourceService.getById(resourceId);

        return R.ok().put("resource", resource);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody ResourceEntity resource){
		resourceService.save(resource);

        return R.ok().put("code","添加成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ResourceEntity resource){
		resourceService.updateById(resource);

        return R.ok().put("code","修改成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Integer[] resourceIds){
		resourceService.removeByIds(Arrays.asList(resourceIds));

        return R.ok().put("code","已成功删除");
    }

//    战绩:通过后台管理房源是否成交，成交的房源展示在列表中，按照时间倒序排列
//    战绩中 分为租房和卖出房屋   进行校验    判断是出租房/二手房,新房
//    在前台获取经济人id,得到本经纪人所售卖/出租的房屋
    @GetMapping("/combatGains/{resourceStatus}")
    public R combatGains(@RequestParam("agentId")Integer agentId,
                         @PathVariable("resourceStatus")Integer resourceStatus){
        return resourceService.combatGains(resourceStatus,agentId);
    }

//    后台管理es审核通过\
    @PostMapping("/egisa")
    public R egisa(@RequestParam("resourceId")Integer resourceId) throws IOException {
        return resourceService.egisa(resourceId);
    }




}
